package com.api.client.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.api.client.model.ApiRequest;
import com.api.client.model.ApiResponse;
import com.api.client.model.HttpStatus;

public final class RequestLogger {

	private RequestLogger() {
	}

	private static Log logger = LogFactory.getLog(RequestLogger.class);

	public static void logRequest(ApiRequest request) {

		String str = "SENT Api Request:\n";

		str += request.getMethod() + " " + request.getUrl() + "\n";

		str += "Headers: " + request.getHeaders() + "\n";

		str += "QueryParams: " + request.getQueryParams() + "\n";

		str += "FormParams: " + request.getFormParams();

		logger.info(str);
	}

	public static void logResponse(ApiResponse response) {

		String str = "RECEIVED Api Response:\n";

		str += response.getMethod() + " " + response.getUrl() + "\n";

		String status = String.valueOf(response.getCode());

		try {
			status = HttpStatus.valueOf(response.getCode()).toString();
		} catch (Exception e) {

			logger.warn("Couldn't log response status", e);
		}

		str += "Status: " + status + "\n";

		str += "Body: " + response.getBody();

		logger.info(str);
	}
}
